package com.example.day_17.adapter;

import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.day_17.R;
import com.example.day_17.bean.BannerBean;
import com.example.day_17.bean.InFoBean;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<BannerBean.DataBean> bannerDatas = new ArrayList<>();
    private List<InFoBean.DataBean.DatasBean> listDatas = new ArrayList<>();
    private static final int BANNER_TYPE = 0;
    private static final int LIST_TYPE = 1;

    public HomeAdapter(Context context) {
        this.context = context;
    }

    public void setBannerDatas(List<BannerBean.DataBean> bannerDatas) {
        this.bannerDatas.addAll(bannerDatas);
        notifyDataSetChanged();
    }

    public void setListDatas(List<InFoBean.DataBean.DatasBean> listDatas) {
        this.listDatas.addAll(listDatas);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == BANNER_TYPE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_banner, viewGroup, false);
            return new ViewHolderBanner(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_list, viewGroup, false);
            return new ViewHolderList(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int type = getItemViewType(i);
        if (type == BANNER_TYPE) {
            ViewHolderBanner viewHolderBanner = (ViewHolderBanner) viewHolder;
            viewHolderBanner.banner.setImages(bannerDatas)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            BannerBean.DataBean dataBean = (BannerBean.DataBean) path;
                            Glide.with(context).load(dataBean.getImagePath()).into(imageView);
                        }
                    }).start();
        } else {
            if (bannerDatas.size() > 0) {
                i = i - 1;
            }
            InFoBean.DataBean.DatasBean datasBean = listDatas.get(i);
            ViewHolderList viewHolderList = (ViewHolderList) viewHolder;
            Glide.with(context).load(datasBean.getEnvelopePic()).into(viewHolderList.iv_pic);
            viewHolderList.tv_name.setText(datasBean.getChapterName());
            viewHolderList.tv_title.setText(datasBean.getTitle());

            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            viewHolderList.tv_time.setText("时间"+format.format(datasBean.getPublishTime()));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && bannerDatas.size() > 0) {//banner有数据
            return BANNER_TYPE;
        } else {
            return LIST_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return bannerDatas.size() > 0 ? listDatas.size() + 1 : listDatas.size();
    }

    public class ViewHolderBanner extends RecyclerView.ViewHolder {
        Banner banner;

        public ViewHolderBanner(@NonNull View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }

    public class ViewHolderList extends RecyclerView.ViewHolder {
        ImageView iv_pic;
        TextView tv_name;
        TextView tv_title;
        TextView tv_time;

        public ViewHolderList(@NonNull View itemView) {
            super(itemView);
            iv_pic = itemView.findViewById(R.id.iv_pic);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }
}
